package com.shengyuan.manage_os.old.ying.erpm;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.shengyuan.manage_os.R;
import com.shengyuan.manage_os.old.ying.boce.task.GenericTask;
import com.shengyuan.manage_os.old.ying.boce.task.TaskAdapter;
import com.shengyuan.manage_os.old.ying.boce.task.TaskListener;
import com.shengyuan.manage_os.old.ying.boce.task.TaskParams;
import com.shengyuan.manage_os.old.ying.boce.task.TaskResult;
import com.shengyuan.manage_os.old.ying.erpm.controls.CurrentTime;
import com.shengyuan.manage_os.old.ying.erpm.controls.JieXieMingCheng;
import com.shengyuan.manage_os.old.ying.erpm.controls.PictureButton;
import com.shengyuan.manage_os.old.ying.erpm.db.Actual_JiShi_Table;
import com.shengyuan.manage_os.old.ying.erpm.tasks.Submit_System_Setting_Data_Task;
import com.shengyuan.manage_os.old.ying.erpm.util.Utility;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

public class Actual_Form_TaiBanKaiShi_JX extends BaseActivity {

	protected PictureButton PictureButton_XiaoShiBiao, PictureButton_YouBiao, PictureButton_Km;
	protected EditText editText_xiaoshibiaoshu,  editText_helishijian;
	protected EditText editText_km,editText_lastxiaoshi;
	
	public static View view;
	private String lastHourString;
	@Override
	public String MyID() {
		if(WorkSpace_TaiBanActivity_JX.ButtonIndex==0)//自有机械
		{
			return  "com.ziyoujixie";
		}else {
			return super.MyID();
		}
		
	}
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.actual_form_taiban_ziyoujixie_kaishi);
		InitControls();
		
		Submit_System_Setting_Data_Task mSubmitTask = new Submit_System_Setting_Data_Task();
		mSubmitTask.setListener(mTaskListener);
		TaskParams params;
		params = new TaskParams();
		params.put(Submit_System_Setting_Data_Task.TYPENAME ,  MyID().replace("com.ying.erpm.", "") );
		params.put(Submit_System_Setting_Data_Task.FIELD , "" );
		params.put(Submit_System_Setting_Data_Task.TYPECODE, JieXieMingCheng.mMachinery.Name);
		mSubmitTask.execute(params);
	}
	
	protected void onCreate(Bundle savedInstanceState, boolean b) {
		super.onCreate(savedInstanceState);
	}

	@Override
	protected void SaveJsonData() {
		IsJson1 = true; mFinished = 0;
		SaveData();
	}
	@Override
	protected void onResume() {
		if (PictureButton_XiaoShiBiao!=null) {
			PictureButton_XiaoShiBiao.postInvalidate();
		}
		if (PictureButton_YouBiao!=null) {
			PictureButton_YouBiao.postInvalidate();	
		}		
		if (PictureButton_Km!=null) {
			PictureButton_Km.postInvalidate();	
		}
		super.onResume();
	}
	protected void SaveData(){
		try {
			JsonMyID();

			getTask();
			getCaiLiao();
			getJieXieMingCheng();
			getFuWuMingCheng();
			
			getZhongLiang();
			
			getGPS();
			getCurrentUser();		
			getMemo();
			JsonPicture();
			JsonPicture_Ext();
			
		}catch (Exception e) {
		}
	}
	
	@Override
	protected void Save_Has_Finished() {
		if (mMaxid>0) {
	        Actual_JiShi_Table dbo= new Actual_JiShi_Table(this);
	        dbo.Save_FirstStartRecord(JieXieMingCheng.mMachinery.Name, CurrentTime.getLongTime(), mMaxid);
	        dbo.close();
		}
	}

	protected void getZhongLiang() throws JSONException {
		if (lastHourString!=null) {
			mJson.put( "上次小时表数:", lastHourString);
		}
		if (editText_xiaoshibiaoshu!=null) {
			mJson.put( getString(R.string.title_actual_form_editText_xiaoshibiaoshu), editText_xiaoshibiaoshu.getText().toString());
		}
		
		if (editText_km!=null) {
			mJson.put( getString(R.string.title_actual_form_editText_km), editText_km.getText().toString());
		}		
		if (editText_helishijian!=null) {
			mJson.put( getString(R.string.title_actual_form_editText_helishijian), editText_helishijian.getText().toString());
		}
	}
	
	protected void JsonPicture_Ext() throws JSONException {
		if (PictureButton_XiaoShiBiao!=null) {
			mJson.put( PictureButton_XiaoShiBiao.FIELD+getString(R.string.title_actual_form_PictureButton), PictureButton_XiaoShiBiao.mFileName);
		}
		if (PictureButton_YouBiao!=null) {
			mJson.put( PictureButton_YouBiao.FIELD+getString(R.string.title_actual_form_PictureButton), PictureButton_YouBiao.mFileName);
		}
	}	
	@Override
	protected void SaveSelfPic(String sPath) {
		try {
			if(PictureButton_Chexing!=null)	CopyPicDest(PictureButton_Chexing, sPath);
			if(PictureButton_XiaoShiBiao!=null)CopyPicDest(PictureButton_XiaoShiBiao, sPath);
			if(PictureButton_YouBiao!=null)CopyPicDest(PictureButton_YouBiao, sPath);
			if(PictureButton_Km!=null)CopyPicDest(PictureButton_Km, sPath);
		} catch (Exception e) {
			Utility.MessageBox(this,e.getMessage());
		}
	}

	@Override
	public void Back() {
		if (view!=null) {
			view.postInvalidate();
		}
		super.Back();
	}

	@Override
	public void InitControls() {
		super.InitControls();
		editText_xiaoshibiaoshu = (EditText) findViewById(R.id.editText_xiaoshibiaoshu);
		editText_lastxiaoshi = (EditText) findViewById(R.id.editText_lastxiaoshi);
		editText_km =(EditText) findViewById(R.id.editText_km);
		editText_helishijian =(EditText) findViewById(R.id.editText_helishijian);
		
		PictureButton_XiaoShiBiao = (PictureButton) findViewById(R.id.PictureButton_XiaoShiBiao);
		PictureButton_Km = (PictureButton) findViewById(R.id.PictureButton_Km);
	}
	
	
	@Override
	public Boolean IsPassed() {
		if (super.IsPassed()) {
 			if(!CheckEditNumber(editText_xiaoshibiaoshu))
				return false;
			if(!CheckEditNumber(editText_km))
				return false;			
			if(!CheckEditNumber(editText_helishijian))
				return false;
				
			if (!PictureButton_Chexing.IsExist()) {
				Utility.MessageBox( this,PictureButton_Chexing.FIELD );
				return false;
			}		
			if (!PictureButton_XiaoShiBiao.IsExist()) {
				Utility.MessageBox(this, PictureButton_XiaoShiBiao.FIELD );
				return false;
			}
			try {
				double d = Double.parseDouble(lastHourString);
				double b = Double.parseDouble(editText_xiaoshibiaoshu.getText().toString());
				if ((b-d)>0.5) {
					if (!CheckEditEmpty(editText_memo)) {
						Utility.MessageBox(this,"不在工作时间用油已经超过0.5小时，请在备注中填写说明情况！");
						return false;
					}
				}
			} catch (Exception e) {
			}
//			if (!PictureButton_YouBiao.IsExist()) {
//				Utility.MessageBox( PictureButton_YouBiao.FIELD );
//				return false;
//			}			
			if (PictureButton_Km!=null && !PictureButton_Km.IsExist()) {
				Utility.MessageBox(this, PictureButton_Km.FIELD );
				return false;
			}			
			return true;
		}else {
			return false;
		}
	}
	
	private TaskListener mTaskListener = new TaskAdapter() {
		@Override
		public void onPostExecute(GenericTask task, TaskResult result) {
			Submit_System_Setting_Data_Task t = (Submit_System_Setting_Data_Task)task;
			if (result == TaskResult.OK) {
				JSONArray jsonArray = t.pJsonArray;
				lastHourString = "";
				try {
					JSONObject jObject = new JSONObject(jsonArray.getJSONObject(0).getString("Json2"));
					lastHourString = jObject.get("小时表数:").toString();
					editText_lastxiaoshi.setText(lastHourString);
				} catch (JSONException e) {
					e.printStackTrace();
				}
			} else {
				Utility.MessageBox(Actual_Form_TaiBanKaiShi_JX.this,t.mErrorMessage);
			}
		}

		@Override
		public void onCancelled(GenericTask task) {
		}

		@Override
		public void onPreExecute(GenericTask task) {
		}
	};

}
